package com.direct.controller;

import com.direct.pojo.Dept;
import com.direct.service.DeptClientService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author caixiaofeng
 * @company xxx公司科技有限公司
 * @create 2020-04-21 15:07
 */
@RestController
public class DeptController {
    @Autowired
    private DeptClientService deptService;
    @Autowired
    private DiscoveryClient discoveryClient;

    @HystrixCommand(fallbackMethod = "error")
    @RequestMapping(value = "dept", method = RequestMethod.POST)
    public void addDept(Dept dept) {
        deptService.addDept(dept);
    }

    @RequestMapping(value = "dept/{deptno}", method = RequestMethod.GET)
    public Dept queryById(@PathVariable(value = "deptno") Long deptno) {
        Dept dept = deptService.queryById(deptno);
        return dept;
    }

    @RequestMapping(value = "depts", method = RequestMethod.GET)
    public List<Dept> queryAll() {
        List<Dept> deptList = deptService.queryAll();
        return deptList;
    }

    @RequestMapping(value = "dept/discovery", method = RequestMethod.GET)
    public Object services() {
        List<String> services = discoveryClient.getServices();
        System.out.println("services = " + services);
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        return this.discoveryClient;
    }
}
